package org.vacoor.xqq.ui.dynamictree;

import org.vacoor.xqq.core.bean.Status;

import javax.swing.tree.MutableTreeNode;
import javax.swing.tree.TreeNode;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

/**
 * User: Vacoor
 */
public class CategoryNode extends ShakeableNode {
    private ContactStatusMonitor monitor = new ContactStatusMonitor();
    private String name;
    private int onlineCount;

    public CategoryNode(int id, String name) {
        super(id);
        setName(name);
    }

    @Override
    public boolean isLeaf() {
        return false;
    }

    @Override
    public void insert(MutableTreeNode newChild, int childIndex) {

        // 为所有可监听属性变化的子元素添加状态监听
        if (newChild instanceof IdentifiableAndObservableDataNode) {
            ((IdentifiableAndObservableDataNode) newChild).addPropertyChangeListener(monitor);
        }
        super.insert(newChild, childIndex);
    }

    @Override
    public void remove(int childIndex) {
        TreeNode node = getChildAt(childIndex);
        if (node instanceof IdentifiableAndObservableDataNode) {
            ( (IdentifiableAndObservableDataNode) node ).removePropertyChangeListener(monitor);
        }
        super.remove(childIndex);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        String old = this.name;
        this.name = name;
        firePropertyChange("name", old, name);
    }

    public int getOnlineCount() {
        return onlineCount;
    }

    public void setOnlineCount(int onlineCount) {
        int old = this.onlineCount;
        this.onlineCount = Math.max(onlineCount, 0);
        this.onlineCount = Math.min(this.getOnlineCount(), getTotalCount());
        firePropertyChange("onlineCount", old, onlineCount);
    }

    public int getTotalCount() {
        return getChildCount();
    }

    @Override
    public String toString() {
        return name +
                " [" + getOnlineCount() +
                "/" + getTotalCount() +
                ']';
    }

    @Override
    protected int doNextShakeStatus() {
        return currentShakeStatus == NORMAL ? HIDDEN : NORMAL;
    }

    private class ContactStatusMonitor implements PropertyChangeListener {

        @Override
        public void propertyChange(PropertyChangeEvent evt) {
            Object oldValue = evt.getOldValue();
            Object newValue = evt.getNewValue();
            if (!(oldValue instanceof Status) || !(newValue instanceof Status) ){
                return;
            }
            IdentifiableAndObservableDataNode n = (IdentifiableAndObservableDataNode) evt.getSource();
            Status oldStatus = (Status) oldValue;
            Status newStatus = (Status) newValue;
            // offline --> online
            if (Status.isOfflineStatus(oldStatus) && !Status.isOfflineStatus(newStatus)) {
                setOnlineCount(getOnlineCount() + 1);
                return;
            }

            // online --> offline
            if (!Status.isOfflineStatus(oldStatus) && Status.isOfflineStatus(newStatus)) {
                setOnlineCount(getOnlineCount() - 1);
                return;
            }
        }
    }

}
